The Code Project
View our advertisers Advertise with us
All Topics, .NET, VB.NET >> VB.NET >> Controls

Auto Complete ComboBox
By Manoh

An article on ComboBox validation. 
  VB.NET
Windows (Win2003, Vista, Win2K, WinXP, Win98, WinME), .NET (.NET 1.0, .NET 1.1, .NET 2.0)
Win32, VS (VS.NET2002, VS.NET2003, VS05)
Dev
  Posted 31 Jul 2005 11:39
Articles by this author
2,430 views
Search:
Toolbox
Broken Article?
VS.NET 2003 from $899
Ultimate Toolbox $499
Print version
Bookmark this
Send to a friend
My Profile
My Settings
My Articles
My Bookmarks
Sign out
 


6 members have rated this article. Result:
Popularity: 1.87. Rating: 2.4 out of 5.

Introduction

This is a simple code snippet which is used to make an Auto Complete ComboBox.

Using the code

Usage: Call the subroutine from within the ComboBox's KeyPress event handler.

AutoComplete(ByRef cb As ComboBox, _
    ByVal e As System.Windows.Forms.KeyPressEventArgs, _
    Optional ByVal blnLimitToList As Boolean = False)
  • cb - ComboBox used to make the Auto Complete ComboBox.
  • e - System.Windows.Forms.KeyPressEventArgs used to handle the keyboard inputs.
  • blnLimitToList - Optional Boolean value indicating whether the user input is limited to the list values or not.

Code

  ' AutoComplete
  Public Sub AutoComplete(ByRef cb As ComboBox, _
      ByVal e As System.Windows.Forms.KeyPressEventArgs, _
      Optional ByVal blnLimitToList As Boolean = False)

    Dim strFindStr As String

    If e.KeyChar = Chr(8) Then 'Backspace
        If cb.SelectionStart <= 1 Then 
            cb.Text = ""
            Exit Sub
        End If
        If cb.SelectionLength = 0 Then
            strFindStr = cb.Text.Substring(0, 
                             cb.Text.Length - 1)
        Else
            strFindStr = cb.Text.Substring(0, 
                          cb.SelectionStart - 1)
        End If
    Else
        If cb.SelectionLength = 0 Then
            strFindStr = cb.Text & e.KeyChar
        Else
            strFindStr = cb.Text.Substring(0, 
                  cb.SelectionStart) & e.KeyChar
        End If
    End If

    Dim intIdx As Integer = -1

    ' Search the string in the Combo Box List.
    intIdx = cb.FindString(strFindStr)

    If intIdx <> -1 Then ' String found in the List.
        cb.SelectedText = ""
        cb.SelectedIndex = intIdx
        cb.SelectionStart = strFindStr.Length
        cb.SelectionLength = cb.Text.Length
        e.Handled = True
    Else
        If blnLimitToList = True Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End If
  End Sub

History

  • Released on July 30th, 2005.

Manoh


Click here to view Manoh's online profile.


Other popular articles:

[Top] Rate this Article for us!     PoorExcellent  

Premium Sponsor

FAQ  Noise tolerance    Search comments  
  View    Per page  
New Message Msgs 1 to 1 of 1 (Total: 1) (Refresh) First Prev Next     
Subject  Author  Date 
  Thanks   Virgil Reboton  7:24 9 Aug '05 
Last Visit: 17:12 Wednesday 21st September, 2005 First Prev Next     

General comment    News / Info    Question    Answer    Joke / Game    Admin message


All Topics, .NET, VB.NET >> VB.NET >> Controls
Updated: 31 Jul 2005 11:39
Article content copyright Manoh, 2005
everything else Copyright © CodeProject, 1999-2005.
Advertise on The Code Project | Privacy

The Ultimate Toolbox • MSDN Communities | ASP Alliance • Developer Fusion • Developersdex • DevGuru • Programmers Heaven • Planet Source Code • Tek-Tips Forums •